package top.lyc829.zzl_android.ui.message.praise;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.io.IOException;
import java.util.List;

import okhttp3.Response;
import top.lyc829.zzl_android.MyApp;
import top.lyc829.zzl_android.logic.bean.NoticeCommentBean;
import top.lyc829.zzl_android.logic.bean.ResultBean;
import top.lyc829.zzl_android.logic.bean.SonCommentBean;
import top.lyc829.zzl_android.logic.bean.SuperCommentBean;
import top.lyc829.zzl_android.logic.database.MyDataBase;
import top.lyc829.zzl_android.logic.database.dao.NoticeCommentDao;
import top.lyc829.zzl_android.logic.network.Network;
import top.lyc829.zzl_android.logic.util.HttpUtil;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.logic.util.gson.JsonUtilNew;
import top.lyc829.zzl_android.ui.message.NoticeType;

public class NoticePraiseViewModel extends AndroidViewModel {

    private NoticeCommentDao noticeCommentDao;

    private MutableLiveData<List<NoticeCommentBean>> noticePraisesLiveData;

    private MutableLiveData<SuperCommentBean> superCommentLiveData;
    private MutableLiveData<List<SonCommentBean>> sonCommentsLiveData;

    public NoticePraiseViewModel(@NonNull Application application) {
        super(application);
        MyDataBase myDataBase = MyDataBase.getInstance(application);
        noticeCommentDao = myDataBase.getNoticeCommentDao();
    }

    public MutableLiveData<List<SonCommentBean>> getSonCommentsLiveData() {
        if (sonCommentsLiveData == null) {
            sonCommentsLiveData = new MutableLiveData<>();
        }
        return sonCommentsLiveData;
    }

    public MutableLiveData<SuperCommentBean> getSuperCommentLiveData() {
        if (superCommentLiveData == null) {
            superCommentLiveData = new MutableLiveData<>();
        }
        return superCommentLiveData;
    }


    public MutableLiveData<List<NoticeCommentBean>> getNoticePraisesLiveData() {
        if (noticePraisesLiveData == null) {
            noticePraisesLiveData = new MutableLiveData<>();
        }
        return noticePraisesLiveData;
    }

    public void loadNoticePraises(int userId) {

        Network.loadNoticePraises(userId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resStr = res.body().string();
                ResultBean<List<NoticeCommentBean>> resultBean = JsonUtilNew.fromJsonArray(resStr, NoticeCommentBean.class);
                if (resultBean.getCode().equals(200)) {
                    mergeData(resultBean.getData());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });

    }

    private void mergeData(List<NoticeCommentBean> data) {
        if (data != null) {
            for (NoticeCommentBean system : data) {
                if (noticeCommentDao.findItemById(system.getId()) == null) {
                    system.setNoticeType(NoticeType.PRAISE);
                    system.setCurrentLoginUserId(Utils.getUserId());
                    noticeCommentDao.insertNoticeComments(system);
                }
            }
        }
        getNoticePraisesLiveData().postValue(noticeCommentDao.getNoticeCommentsByNoticeTypeAndUserId(NoticeType.PRAISE, Utils.getUserId()));
    }

    public void loadSuperComment(int commentId, int userId) {
        Network.loadCommentById(commentId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resStr = res.body().string();
                ResultBean<SuperCommentBean> resultBean = JsonUtilNew.fromJsonObject(resStr, SuperCommentBean.class);
                if (resultBean.getCode().equals(200)) {
                    getSuperCommentLiveData().postValue(resultBean.getData());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }

    public void loadSonComments(int commentId, int page) {
        Network.getSonComments(commentId, page, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resultStr = res.body().string();
                Utils.Sout(resultStr);
                ResultBean<List<SonCommentBean>> resultBean = JsonUtilNew.fromJsonArray(resultStr, SonCommentBean.class);
                if (resultBean.getCode().equals(200)) {
                    getSonCommentsLiveData().postValue(resultBean.getData());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }

    public void deleteItem(NoticeCommentBean commentBean) {
        if (commentBean != null) {
            new Thread(() -> {
                noticeCommentDao.deleteNoticeComments(commentBean);
            }).start();
        }
    }


}
